﻿using GN.Pay.TradeChannel.DefaultImpl;
using GN.Pay.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.YJPay.Response
{
    [Serializable]
    class RefundResponse : AResponse
    {
        private readonly ITradeChannel channel;
        private readonly YJPayConfigure config;
        private readonly IRefundApplyInfo apply;

        internal RefundResponse(string responseString,
            ITradeChannel channel,
            YJPayConfigure config,
            IRefundApplyInfo apply)
        {
            if (responseString != null)
            {
                this.ResponseString = responseString.Trim();
            }
            else
            {
                this.ResponseString = responseString;
            }
            this.channel = channel;
            this.config = config;
            this.apply = apply;
        }

        /// <summary>
        /// 获取响应字符
        /// </summary>
        public string ResponseString { get; private set; }

        /// <summary>
        /// 获取是否成功
        /// </summary>
        public bool IsSuccess { get; private set; }

        /// <summary>
        /// 获取错误代码
        /// </summary>
        public string ErrorCode { get; private set; }

        /// <summary>
        /// 转为结果
        /// </summary>      
        /// <returns></returns>
        public ITradeRefundApplyResult ToResult()
        {
            var result = ResponseEntity.Create(ResponseString);
            var state = TradeApplyResultState.Apply_Success_Wiat_Notify;
            var msg = "OK";           
            if (!((result.resultCode.Equals("EXECUTE_SUCCESS", StringComparison.InvariantCultureIgnoreCase)
                || result.resultCode.Equals("EXECUTE_PROCESSING", StringComparison.InvariantCultureIgnoreCase))
                && result.success))
            {
                state = TradeApplyResultState.Apply_Error;
                msg = "(" + (result.resultCode != null ? result.resultCode : "") + ")" + (result.resultMessage != null ? result.resultMessage : "");
            }
            return new TradeRefundApplyResult(
                this.channel, apply.RefundNo, apply.TradeNo, apply.ApplyMoney,
                0m, null, state, msg);
        }
    }
}
